<template>
  <div>
    <!-- 添加 -->
    <el-button type="primary" @click="addClick">添加</el-button>
    <!-- 表格数据 -->
    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="分类名称"></el-table-column>
        <el-table-column label="分类级别">
          <div>顶级分类</div>
        </el-table-column>
        <el-table-column prop="wap_banner_url" label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.wap_banner_url" height="90" alt />
          </template>
        </el-table-column>
        <el-table-column prop="is_show" label="是否显示"></el-table-column>
        <el-table-column prop="sort_order" label="排序"></el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
import { GetCategory, GetTopCategory } from '../../request/api'
export default {
  data() {
    return {

    }
  },
  computed: {
    tableData() {
      let tableData = this.$store.state.category.categoryList
      for (var i = 0; i < tableData.length; i++) {
        // "是"、"否"转换
        tableData[i].is_show == 0 ? tableData[i].is_show = "否" : tableData[i].is_show = "是"
      }
      console.log("分类", tableData)
      return tableData
    }
  },

  methods: {
    // 点击编辑
    handleClick(row) {
      console.log("row", row);
      this.$router.push("/editcategory/" + row.id)
    },

    // 点击添加数据
    addClick() {
      // console.log("点击了添加")
      this.$router.push("/addcategory")
    },

  },

  created() {

    // 通过获取商品的api接口获取到的数据存到vuex里面
    GetCategory({
      headers: { 'X-Nideshop-Token': localStorage.getItem('token') },
    }).then(res => {
      console.log("get/admin/category")
      this.$store.commit('saveCategoryData', res.data)
    })

    GetTopCategory({
      headers: { 'X-Nideshop-Token': localStorage.getItem('token') },
    }).then(res => {
      console.log("顶级分类数据", res)

    })
  }

}
</script>

<style lang="less" scoped>
.input {
  margin-bottom: 10px;
  width: 200px;
}
.el-pagination {
  text-align: right;
}
</style>